public class Test {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode pA = headA;
        ListNode pB = headB;

        int lenA = 0,lenB = 0;
        while (pA != null){
            pA = pA.next;
            lenA++;
        }
        while (pB != null){
            pB = pB.next;
            lenB++;
        }

        int len = lenA - lenB;
        if (len < 0){
            pA = headB;
            pB = headA;
            len = lenB - lenA;
        } else {
            pA = headA;
            pB = headB;
        }

        while (len != 0){
            pA = pA.next;
            len--;
        }

        while (pA != null && pB != null && pA != pB){
            pA = pA.next;
            pB = pB.next;
        }

        return pA;
    }
}
